<?php

class AuthController extends Zend_Controller_Action
{

    protected $_redirectUrl = '/';

    public function init()
    {
    	
    }

    public function indexAction()
    {
    	if (null === Zend_Auth::getInstance()->getIdentity()) {
    		$this->_helper->redirector->gotoRouteAndExit(array('action'=>'form'));
    	} else {
    		$this->_redirect($_redirectUrl);
    	}
    }

    public function formAction()
    {
        $form = new Application_Form_Login();
        $form->setAction($this->view->baseUrl() .'/auth/form/');
        $this->view->formResponse = '';
        if ($this->_request->isPost()) {
        	if ($form->isValid($_POST)) {
        		$authAdapter = $form->username->getValidator('Authorise')->getAuthAdapter();
        		
        		$data = $authAdapter->getResultRowObject(null, 'password');
        		$auth = Zend_Auth::getInstance();
        		$auth->getStorage()->write($data);
        		$this->_redirect($this->_redirectUrl);
        	} else {
        		$auth = Zend_Auth::getInstance();
        		$auth->clearIdentity();
        		
        		$this->view->formResponse = 'Sorry, there was a problem with your submission. Please check the following:';
        		$form->populate($_POST);
        	}
        }
        $this->view->form = $form;
    }

    public function logoutAction()
    {
    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity();
    	$this->_redirect($this->_redirectUrl);
    }

    public function privilegesAction()
    {
    	
    }


}







